package com.scitc.pleasurevideo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scitc.pleasurevideo.base.ETActivity;
import com.scitc.pleasurevideo.helper.ForwardHelper;
import com.scitc.pleasurevideo.helper.ThreadHelper;
import com.scitc.pleasurevideo.util.AtyContainer;
import com.scitc.pleasurevideo.x5webview.X5WebView;
import com.tencent.smtt.export.external.interfaces.WebResourceError;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.Timer;
import java.util.TimerTask;

public class ParsingActivity extends ETActivity {
    private X5WebView mX5WebView;
    private ProgressBar progressBar;
    private TextView Parsingtext;
    private View viewLine;

    private String[] playWebUrlArray = {"youku.com/v_", "youku.com/v", "youku.com/a",
            "iqiyi.com/v_", "iqiyi.com/w_", "iqiyi.com/a_", "iqiyi.com/dianying/",
            "qq.com/x/page/", "qq.com/play", "qq.com/cover", "qq.com/x/m/play"};


    Timer timer = new Timer();

    /**
     * (2) 使用handler处理接收到的消息
     */
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {

                if (TextUtils.isEmpty(mX5WebView.getUrl())) {
                    return;
                }

                boolean judge = false;
                String webUrl = mX5WebView.getUrl();
                for (int i = 0; i < playWebUrlArray.length; i++) {
                    if (webUrl.indexOf(playWebUrlArray[i]) >= 0) {
                        judge = true;
                        break;
                    }
                }

                if (judge) {
                    if (Parsingtext.getVisibility() == View.GONE) {
                        Parsingtext.setVisibility(View.VISIBLE);
                        viewLine.setVisibility(View.VISIBLE);
                    }
                } else {
                    Parsingtext.setVisibility(View.GONE);
                    viewLine.setVisibility(View.GONE);
                }

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parsing);

        String url = getIntent().getStringExtra("url");

        mX5WebView = (X5WebView) findViewById(R.id.x5WebView);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        Parsingtext = (TextView) findViewById(R.id.Parsingtext);
        viewLine = findViewById(R.id.viewLine);


        mX5WebView.loadUrl(url);


        initX5WebView();
        setProgressBar();
        setClick();

        setTimer();
    }

    private void setTimer() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.what = 0;
                mHandler.sendMessage(message);
            }
        }, 1000, 1000);
    }

    /**
     * 启用硬件加速
     */
    private void initX5WebView() {

        // 修改ua使得web端正确判断
        String ua = mX5WebView.getSettings().getUserAgentString();
        mX5WebView.getSettings().setUserAgentString(ua + "; aaa");


        try {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                getWindow()
                        .setFlags(
                                android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                                android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception e) {
        }
    }

    /**
     * 设置进度条
     */
    private void setProgressBar() {
        mX5WebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                //显示进度条
                if (newProgress < 100) {
                    progressBar.setProgress(newProgress);
                    progressBar.setVisibility(View.VISIBLE);
                } else {
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 点击事件
     */
    private void setClick() {
        mX5WebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
                //点击事件
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                //加载错误
            }
        });
        Parsingtext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                String url="https://jx.iztyy.com/svip/?url="+mX5WebView.getUrl();
                String url = "http://api.mcncn.cn/?url=" + mX5WebView.getUrl();
                timer.cancel();
                ForwardHelper.toWebviewPlayActivity(mInstance, url, "");

            }
        });
    }

    /**
     * 返回键监听
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mX5WebView != null && mX5WebView.canGoBack()) {
                mX5WebView.goBack();
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        //释放资源
        if (mX5WebView != null)
            mX5WebView.destroy();
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setTimer();
    }
}
